package com.dyzs.client;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

import com.dyzs.util.Constants;
import com.dyzs.util.WSUtils;

public class WordClient {
	public String updateList(String city,String title,String page) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", city);		
		params.put("arg1", title);
		params.put("arg2", page);	
		try {
			String resultXml = WSUtils.callWS("getGuideWordService",params, Constants.WordURL);			
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw  e;
		}
	}

	
	
	public String updateDetail(String wordId) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", wordId);			
		try {
			String resultXml = WSUtils.callWS("getWordInfo",params, Constants.WordURL);			
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw  e;
		}
	}

	public String getWordInfoForPlan(String wordId) throws Exception {
		Map<String, Object> params = new TreeMap<String, Object>();
		params.put("arg0", wordId);			
		try {
			String resultXml = WSUtils.callWS("getWordInfoForPlan",params, Constants.WordURL);			
			return resultXml;
		} catch (Exception e) {
			e.printStackTrace();
			throw  e;
		}
	}
	
	
	
	
	public Map<String,Object> pullWordInfoForPlan(String resultXml) throws Exception {
		if(resultXml.equals("W")){
			throw new Exception("服务端异常！");
		}else{	
			Map<String, Object> fatherMap =null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {				
					switch (event) {
						case XmlPullParser.START_DOCUMENT:
							fatherMap = new HashMap<String, Object>();							
							break;
						case XmlPullParser.START_TAG:
							if(!parser.getName().equals("root"))
								fatherMap.put(parser.getName().toLowerCase(), parser.nextText());
							break;								
						}
					event = parser.next();
				}

			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}
			return fatherMap;			
		}
	}
	
	
	
	public Map<String,Object> pullListXml(String resultXml) throws Exception {
		if(resultXml.equals("W")){
			throw new Exception("服务端异常！");
		}else{				
			
			List<Map<String,Object>> showList=null;
			Map<String, Object> fatherMap =null;
			Map<String, Object> childMap=null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {				
					switch (event) {
						case XmlPullParser.START_DOCUMENT:
							fatherMap = new HashMap<String, Object>();							
							break;
						case XmlPullParser.START_TAG:
							if ("totalNum".equals(parser.getName())) {
								fatherMap.put("totalNum", parser.nextText());
							}else if ("lists".equals(parser.getName())) {
								showList=new ArrayList<Map<String,Object>>();
							}else  if ("item".equals(parser.getName())) {
								childMap = new HashMap<String, Object>();	
							}else 
							if(childMap!=null){
								if ("WORD_ID".equals(parser.getName())) {
									childMap.put("wordId", parser.nextText());
								} else if ("TITLE".equals(parser.getName())) {
									childMap.put("title", parser.nextText());
								} 
							}						
							break;
						case XmlPullParser.END_TAG:
							if ("item".equals(parser.getName())) {							
								showList.add(childMap);						
								childMap = null;						
							}
							if ("lists".equals(parser.getName())) {							
								fatherMap.put("list", showList);					
							}
							break;
						}
					event = parser.next();
				}

			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}
			return fatherMap;			
		}
	}
	
	
	
	
	public Map<String,Object> pullDetailXml(String resultXml) throws Exception {
		if(resultXml.equals("W")){
			throw new Exception("服务端异常！");
		}else{	
			Map<String, Object> fatherMap =null;
			try {
				ByteArrayInputStream bin = new ByteArrayInputStream(resultXml.getBytes());
				InputStreamReader in = new InputStreamReader(bin);
				XmlPullParser parser = Xml.newPullParser();
				parser.setInput(in);
				int event = parser.getEventType();
				while (event != XmlPullParser.END_DOCUMENT) {				
					switch (event) {
						case XmlPullParser.START_DOCUMENT:
							fatherMap = new HashMap<String, Object>();							
							break;
						case XmlPullParser.START_TAG:							
//							String c=parser.getName();							
//							String t=parser.nextText();
						
							if ("SCEN_NAME".equals(parser.getName())) {
								fatherMap.put("scen_name", parser.nextText());
							}else if ("TITLE".equals(parser.getName())) {
								fatherMap.put("title", parser.nextText());
							}else  if ("SIG_NAME".equals(parser.getName())) {
								fatherMap.put("sig_name", parser.nextText());								
							}else  if ("CONTENT".equals(parser.getName()))
								fatherMap.put("content", parser.nextText());						
							break;	
							
						}
					event = parser.next();
				}

			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("解析失败！", e);
			}
			return fatherMap;			
		}
	}
}
